package com.promote.algorithm;

import java.util.Arrays;
import java.util.HashSet;

/**
 * @ClassName ReverseVowels
 * Description 反转字符串中的元音字符
 * @Author LiZiHao
 * Date 2021/6/22 10:05
 * @Version 1.0
 **/
public class ReverseVowels {

    private final static HashSet<Character> vowels = new HashSet<>(
            Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));

    public static String reverse(String s) {
        if (s == null) {
            return null;
        }
        int i = 0;
        int j = s.length() - 1;
        final char[] result = new char[s.length()];
        while (i <= j){
            final char ci = s.charAt(i);
            final char cj = s.charAt(j);
            if (!vowels.contains(ci)) {
                result[i++] = ci;
            } else if (!vowels.contains(cj)) {
                result[j--] = cj;
            } else {
                result[i++] = cj;
                result[j--] = ci;
            }
        }
        return new String(result);
    }

    public static void main(String[] args) {

        String s = "leetcode";
        final String result = reverse(s);
        System.out.println(result);
    }
}
